import { Component } from '@angular/core';
import { NavController, AlertController } from 'ionic-angular';
import { AuthService } from '../../../../providers/auth-service';
import io from 'socket.io-client'

@Component({
  selector: 'page-my-chat',
  templateUrl: 'chat.html'
})

export class MyChatPage {
  user:any
  messages = []
  socket: any
  inputMessage = ''
  constructor(public navCtrl: NavController,
   private auth: AuthService,
   private alertCtrl: AlertController
   ) {
    this.user =  auth.getUserInfo().user
    this.socket = io.connect('http://127.0.0.1:8080');
    this.socket.on('message', message => {
      this.messages.push(message)
    })
  }
  public sendMessage() {
    let message = {
      username: this.user.username,
      id: this.user.id,
      message: this.inputMessage
    } 
    this.socket.emit('message', message)
    this.inputMessage = ''
  }
}
